#!/usr/bin/perl
use XML::Simple;
use Data::Dumper;
# appa-xml2csv - takes the xml output from appa and converts it to a
# comma separated file for import into excel.
# Specific 
my $inputfile='appc_feb08.xml';
my $stafffile='appc_feb08_staff.csv';
my $clientfile='appc_feb08_client.csv';
my $debug=1;
$xml = new XML::Simple (KeyAttr=>[]);
$data = $xml->XMLin($inputfile);
print Dumper($data) if ($debug>4);
open STAFFFILE,">",$stafffile;
open CLIENTFILE,">",$clientfile;


# dereference hash ref
# access <entry> array
# lovingly stolen from http://articles.techrepublic.com.com/5100-10878_11-5363190.html
foreach $entry (@{$data->{entry}})
{
  $company = $entry->{name};
  @staff = split (/\n/, $entry->{staff});
  @client = split (/\n/, $entry->{paclient}.$entry->{monclient});
  
  for ($i=$#client;$i>=0;$i--) {
    if ($client[$i] =~ m/None/) {
      print "\@client was (".join (',',@client).")\n" if $debug>3;
      print "Removing #$i, $client[$i].\n" if $debug;
      splice (@client,$i,1);
      print "\@client is now (".join (',',@client).")\n " if $debug>3;
    }
  }

  foreach $i (@staff) {print STAFFFILE "\"$i\",\"$company\"\n";}
  foreach $i (@client) {print CLIENTFILE "\"$i\",\"$company\"\n";}
}


#print OUTFILE $csv;

close STAFFFILE;
close CLIENTFILE;